package common

import (
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/http_server/backend_controller"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/errcode"
	dto "git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/dto/common"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/utils"
	"git.mycaigou.com/gfyx/micro-gfyx-api/service/common"
	"github.com/gin-gonic/gin"
)

type SiteAnnouncementController struct {
	backend_controller.BackendBaseController
	siteAnnouncementService common.SiteAnnouncementServiceIface
}

func NewSiteAnnouncementController(siteAnnouncementService common.SiteAnnouncementServiceIface) *SiteAnnouncementController {
	return &SiteAnnouncementController{siteAnnouncementService: siteAnnouncementService}
}

func (c *SiteAnnouncementController) RegisterRouter(g *gin.RouterGroup) {
	// 保存弹窗公告
	g.POST("/common/site-announcement/save", c.SaveSiteAnnouncement)
	// 获取弹窗公告管理列表
	g.GET("/common/site-announcement/manage-list", c.GetSiteAnnouncementManageList)
	// 获取弹窗公告详情
	g.GET("/common/site-announcement/detail", c.GetSiteAnnouncementDetail)
	// 删除弹窗公告
	g.POST("/common/site-announcement/delete", c.DeleteSiteAnnouncement)
}

func (c *SiteAnnouncementController) SaveSiteAnnouncement(ctx *gin.Context) {
	request := &dto.SiteAnnouncementSaveRequest{}
	if err := ctx.ShouldBindJSON(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	response, err := c.siteAnnouncementService.SaveSiteAnnouncement(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}

func (c *SiteAnnouncementController) GetSiteAnnouncementManageList(ctx *gin.Context) {
	request := &dto.SiteAnnouncementManageListRequest{}
	if err := ctx.ShouldBindQuery(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	if request.Page == 0 {
		request.Page = 1
	}
	if request.PageSize == 0 {
		request.PageSize = 20
	}
	response, err := c.siteAnnouncementService.GetSiteAnnouncementManageList(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}

func (c *SiteAnnouncementController) GetSiteAnnouncementDetail(ctx *gin.Context) {
	request := &dto.SiteAnnouncementDetailRequest{}
	if err := ctx.ShouldBindQuery(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	if request.Id <= 0 {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, "id不能为空")
		return
	}
	response, err := c.siteAnnouncementService.GetSiteAnnouncementDetail(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}

func (c *SiteAnnouncementController) DeleteSiteAnnouncement(ctx *gin.Context) {
	request := &dto.SiteAnnouncementDeleteRequest{}
	if err := ctx.ShouldBindJSON(request); err != nil {
		c.Fail(ctx, errcode.COMMON_PARAMS_ERROR, utils.GetValidateErr(request, err))
		return
	}
	response, err := c.siteAnnouncementService.DeleteSiteAnnouncement(ctx, request)
	if err != nil {
		grpcErr := rpc_client.GetGrpcError(err)
		c.Fail(ctx, grpcErr.GetCode(), grpcErr.GetMessage())
		return
	}
	c.Success(ctx, response)
}
